<template>
    <div class="goods-comment">
      <div class="head">
        <div class="data">
          <p><span>{{commentInfo.salesCount}}</span><span>人购买</span></p>
          <p><span>{{commentInfo.praisePercent}}</span><span>好评率</span></p>
        </div>
        <div class="tags">
          <div class="dt">大家都在说：</div>
          <div class="dd">
            <a v-for="(item,i) in commentInfo.tags" :key="item.title" href="javascript:;" :class="{active:currTagIndex===i}" @click="changeTag(i)">
              {{item.title}}（{{item.tagCount}}）
            </a>
          </div>
        </div>
      </div>
      
      <!-- 排序 -->
      <div class="sort" v-if="commentInfo">
        <span>排序：</span>
         <a
            @click="changeSort(null)"
            href="javascript:;"
            :class="{active:reqParams.sortField===null}"
          >默认</a>
          <a
            @click="changeSort('praiseCount')"
            href="javascript:;"
            :class="{active:reqParams.sortField==='praiseCount'}"
          >最热</a>
          <a
            @click="changeSort('createTime')"
            href="javascript:;"
            :class="{active:reqParams.sortField==='createTime'}"
          >最新</a>
      </div>
  
      <!-- <div class="list">
        <div class="item" v-for="item in getlist" :key="item.id">
          <div class="user">
            <img :src="item.member.avatar" alt="">
            <span></span>
          </div>
          <div class="body">
            <div class="score">
              <i class="iconfont icon-wjx01"></i>
              <i class="iconfont icon-wjx01"></i>
              <i class="iconfont icon-wjx01"></i>
              <i class="iconfont icon-wjx01"></i>
              <i class="iconfont icon-wjx02"></i>
              <span class="attr">颜色：黑色 尺码：M</span>
            </div>
            <div class="text">网易云app上这款耳机非常不错 新人下载网易云购买这款耳机优惠大 而且耳机🎧确实正品 音质特别好 戴上这款耳机 听音乐看电影效果声音真是太棒了 无线方便 小盒自动充电 最主要是质量好音质棒 想要买耳机的放心拍 音效巴巴滴 老棒了</div>
            <div class="time">
              <span>2020-10-10 10:11:22</span>
              <span class="zan"><i class="iconfont icon-dianzan"></i>100</span>
            </div>
          </div>
        </div>
      </div> -->


      <div class="list" v-if="evaluate2 && evaluate2.items">
			<div class="item" v-for="(item, index) in evaluate2.items" :key="index">
				<div class="user">
					<img :src="item.member.avatar" alt="" />
					<span>{{ item.member.nickname }}</span>
				</div>
				<div class="body">
					<div class="score" v-if="item.score && item.orderInfo.specs">
						<span v-if="item.score && item.orderInfo.specs"><i class="iconfont icon-wjx01" v-for="i in item.score" :key="i"></i></span>
						<span v-if="item.score && item.orderInfo.specs"><i class="iconfont icon-wjx02" v-for="i in 5 - item.score" :key="i"></i></span>
						<span class="attr" v-for="(it, key) in item.orderInfo.specs" :key="key">{{ it.name }}:{{ it.nameValue }}</span>
					</div>
					<div class="text">
						{{ item.content }}
					</div>
					<div class="time">
						<span>{{ item.orderInfo.createTime }}</span>
						<span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
					</div>
				</div>
			</div>
		</div>
    <!-- 分页组件 -->
    <XtxPaginaion></XtxPaginaion>
    </div>
  </template>
    <script>
    import XtxPaginaion from '@/components/utils/XtxPagination.vue'
  import { getEval } from '@/utils/api/detail'
  import qs from "querystring"
  import axios from "axios"
  export default {
    name: 'GoodsComment',
    props: ['infoobj'],
    components:{
      XtxPaginaion
    },
    data() {
      return {
        commentInfo: [],
        currTagIndex: 0,
        reqParams: {
          page: 1,
          pageSize: 10,
          hasPicture: null,
          tag: null,
          sortField: null
        },
        evaluate:[],
        evaluate2:[]
      }
    },
    methods: {
      async getlist() {
        let res = await getEval(this.infoobj.id)
       // console.log(res);
        res.result.tags.unshift({ type: 'img', title: '有图', tagCount: res.result.hasPictureCount })
        res.result.tags.unshift({ type: 'all', title: '全部评价', tagCount: res.result.evaluateCount })
        this.commentInfo = res.result
        console.log(res);
       // console.log(this.commentInfo);
      },
      changeTag(i) {
        this.currTagIndex = i
          // 设置有图和标签条件
       const currTag = this.commentInfo.tags[i]
       if (currTag.type === 'all') {
         this.reqParams.hasPicture = false
         this.reqParams.tag = null
       } else if (currTag.type === 'img') {
         this.reqParams.hasPicture = true
         this.reqParams.tag = null
       } else {
         this.reqParams.hasPicture = false
         this.reqParams.tag = currTag.title
       }
       this.reqParams.page = 1
      },
      changeSort(type){
        this.reqParams.sortField=type
        this.reqParams.page=1
      },
    //  //处理数据 昵称加*号 规格拼接字符串
    //  formatSpecs(specs){
    //   specs.reduce((p,c)=>
    //     `${p}${c.name}:${c.nameValue}`,
    //   ).trim()
    //  },
    //  formatNickname(nickname){
    //   nickname.substr(0,1)+'****'+nickname.substr(-1)
    //  }
     // 评论分类
     async xuan3(id) {
      let res = await axios.get(
        `https://mock.boxuegu.com/mock/1175/goods/${id}/evaluate`
      );
      // console.log(ressss);
      this.evaluate = res.data.result;
    },
    async xuan4(id) {
      let res = await axios.get(
        `https://mock.boxuegu.com/mock/1175/goods/${id}/evaluate/page?${qs.stringify(
          this.query
        )}`
      );
      // console.log(resst);
      console.log(res);
      this.evaluate2 = res.data.result;

    },
    },
    mounted() {
      this.getlist()
      this.xuan3(this.$route.query.id)
      this.xuan4(this.$route.query.id)
    },
    watch: {
    reqParams:{
       async   handler(){
       // console.log(val)
       let res = await getEval(this.infoobj.id,this.reqParams)
       this.commentInfo = res.result
      //console.log(this.commentInfo)
        },
        deep:true,
        immediate:true
      }
    },
  }
  </script>
  <style scoped lang="scss">
  @import "@/assets/css/variables.scss";
  .goods-comment {
    .head {
      display: flex;
      padding: 30px 0;
      .data {
        width: 340px;
        display: flex;
        padding: 20px;
        p {
          flex: 1;
          text-align: center;
          span {
            display: block;
            &:first-child {
              font-size: 32px;
              color: $priceColor;
            }
            &:last-child {
              color: #999;
            }
          }
        }
      }
      .tags {
        flex: 1;
        display: flex;
        border-left: 1px solid #f5f5f5;
        .dt {
          font-weight: bold;
          width: 100px;
          text-align: right;
          line-height: 42px;
        }
        .dd {
          flex: 1;
          display: flex;
          flex-wrap: wrap;
          > a {
            width: 132px;
            height: 42px;
            margin-left: 20px;
            margin-bottom: 20px;
            border-radius: 4px;
            border: 1px solid #e4e4e4;
            background: #f5f5f5;
            color: #999;
            text-align: center;
            line-height: 40px;
            &:hover {
              border-color: $xtxColor;
              background: lighten($xtxColor, 50%);
              color: $xtxColor;
            }
            &.active {
              border-color: $xtxColor;
              background: $xtxColor;
              color: #fff;
            }
          }
        }
      }
    }
    .sort {
      height: 60px;
      line-height: 60px;
      border-top: 1px solid #f5f5f5;
      border-bottom: 1px solid #f5f5f5;
      margin: 0 20px;
      color: #666;
      > span {
        margin-left: 20px;
      }
      > a {
        margin-left: 30px;
        &.active,
        &:hover {
          color: $xtxColor;
        }
      }
    }
  }
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
  </style>